﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BitPortal.Models;
using BitPortal.Business;
using BitPortal.Utils;

namespace BitPortal.Modules.ProductDetail
{
    public partial class ProductDetail : Controls.BaseUserControl
    {
        public bool EnabledDetail = true;
        public bool EnabledComment = true;
        public bool EnabledReference = true;
        public bool EnabledZoom = true;
        public int Zoom = 400;
        public bool EnabledStatistic = true;
        public bool EnabledShare = true;
        public Models.Product CurrentProduct { set; get; }
        public IQueryable<Media> ListImages { get { return CurrentProduct.GetMedia("Image", true); } }
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected override void OnInit(EventArgs e)
        {
            CurrentProduct = ProductBLL.GetByID(Request.QueryString["ProductID"].ToInt64TryParse());
            if (CurrentProduct == null) ExceptionHander.ThrowObjectNotFoundException();
            base.OnInit(e);
            //Load Css, Js file
            (this.Page as Controls.BaseFrontendPage).IncludeCssFile("/jquery-ui.custom/css/smoothness/jquery-ui-1.8.16.custom.css");
            (this.Page as Controls.BaseFrontendPage).IncludeScriptFile("/jquery-ui.custom/js/jquery-ui-1.8.16.custom.min.js");
            //Load Data
            ltrTitle.Text = CurrentProduct.Name;
            this.Page.Title = CurrentProduct.Meta.MetaTitle;
            this.Page.MetaDescription = CurrentProduct.Meta.MetaDescription;
            this.Page.MetaKeywords = CurrentProduct.Meta.MetaKeyword;
            //Load Config
            EnabledComment = (bool)PortalConfigBLL.GetValueByName("Product.ProductDetail.EnabledComment");
            EnabledDetail = (bool)PortalConfigBLL.GetValueByName("Product.ProductDetail.EnabledDetail");
            EnabledReference = (bool)PortalConfigBLL.GetValueByName("Product.ProductDetail.EnabledReference");
            EnabledZoom = (bool)PortalConfigBLL.GetValueByName("Product.ProductDetail.EnabledZoom");
            Zoom = PortalConfigBLL.GetValueByName("Product.ProductDetail.Zoom").ToInt32TryParse();
            //Share
            EnabledShare = (bool)PortalConfigBLL.GetValueByName("Product.ProductDetail.EnabledShare");
            if (!EnabledShare) { this.SocialShare.Visible = false; this.SocialShare1.Dispose(); }
            //Statistic
            EnabledStatistic = (bool)PortalConfigBLL.GetValueByName("Product.EnabledStatistic");
            this.Statistic1.CurrentStatistic = CurrentProduct.Statistic;
            if (!EnabledStatistic)
            {
                this.Statistic1.Dispose();
            }
            //Comment
            if (EnabledComment)
            {
                this.ProductComment.DiscusionGroupReference = CurrentProduct.DiscussionGroup;
            }
            else
            {
                this.ProductComment.Dispose();
            }
            //Products Reference
            if (!EnabledReference) { this.dtlProduct.Dispose(); return; }

            bool IsHot = (bool)PortalConfigBLL.GetValueByName("Product.ProductDetail.ReferenceHot");
            bool IsNew = (bool)PortalConfigBLL.GetValueByName("Product.ProductDetail.ReferenceNew");

            List<string> conditions = new List<string>();
            conditions.Add("1-True-IsEnabled-eq");
            conditions.Add("5-" + CurrentProduct.ProductID + "-ProductID-neq");
            if (IsHot) conditions.Insert(0, "1-True-IsHot-eq");
            if (IsNew) conditions.Insert(0, "1-True-IsNew-eq");

            int NumberOfRows = PortalConfigBLL.GetValueByName("Product.ProductDetail.ReferenceRows").ToInt32TryParse();
            int NumberOfColumns = PortalConfigBLL.GetValueByName("Product.ProductDetail.ReferenceCols").ToInt32TryParse();
            this.dtlProduct.RepeatColumns = NumberOfColumns;

            List<Product> listProduct = new List<Product>();
            listProduct = ProductBLL.GetByCategory(CurrentProduct.CategoryID);
            this.dtlProduct.DataSource = listProduct.DataProcessing(conditions: conditions, sort: "SortOrder", dir: "desc", start: 0, limit: NumberOfColumns * NumberOfRows);
            this.dtlProduct.DataBind();
        }

    }
}